<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$msg = "";
  $field = NULL;
  if(isset($_REQUEST['deliveryChallanMasterId']))
  {
    // here is the code to insert data in  masterdelivery:Start//
    $currentDate = date("Y-m-d");
    $masterDeliveryId = 0;
    $partyId = substr_replace($_REQUEST['partyId'],"",-1);
    if(substr($_REQUEST['partyId'],-1) == 'P')
    {
    	$partyField = $partyId;
    	$supplierField = 0;
    }
    else
    {
      $partyField = 0;
    	$supplierField = $partyId;
    }
    
	  $selectMaxSr = "SELECT MAX(fmSrNo) + 1 AS fmSrNo FROM masterdelivery ";
    $selectMaxSrRes = mysql_query($selectMaxSr);
    if($srRow = mysql_fetch_array($selectMaxSrRes))
    {
    	if($srRow['fmSrNo'] == NULL)
    	  $srRow['fmSrNo'] = 1;
    	  
    	$fmSrNo = $srRow['fmSrNo'];
    }
     
    $insertMasterDelivery = "INSERT INTO masterdelivery(fmSrNo, dateMaster, partyId, supplierId)
                             VALUES ('".$fmSrNo."','".$currentDate."',".$partyField.",".$supplierField.")";
    $insertMasterDeliveryRes = mysql_query($insertMasterDelivery);
    if(!$insertMasterDeliveryRes)
    {
    	$msg = "error";  //To Print the message only when error occurs from query failed.
    }
    else
    {
    //	header("Location:masterDeliveryList.php?msg=1");
    }
    $masterDeliveryId = mysql_insert_id();
    // here is the code to insert data in  masterdelivery:End//
    
    // here Update Query For deliverychallandetail,deliverychallanmaster Table:Start//
    
    //// Commented because required later
    foreach ($_POST['deliveryChallanMasterId'] as $key => $value)
    {
      $updateDeliveryChallanMaster  = "UPDATE deliverychallanmaster
                                          SET status = 'FM'
                                        WHERE deliveryChallanMasterId =".$value;
      $updateDeliveryChallanMasterRes = mysql_query($updateDeliveryChallanMaster);
    }
    
    if(isset($_POST['deliveryChallanDetailId']))
    {
      foreach ($_POST['deliveryChallanDetailId'] as $deliveryKey => $deliveryValue)
      {
        $quantityTaken = $_POST['qtyTaken'][$deliveryKey];
        $msterid = $_POST['deliveryChallanDetailId'][$deliveryKey];
        $updateDeliveryChallanDetail  = "UPDATE deliverychallandetail
                                            SET masterDeliveryId = ".$masterDeliveryId.",
                                                fmQuantity = fmQuantity + ".$quantityTaken.",
                                                remainQtyAftrFM = remainQtyAftrFM - ".$quantityTaken.",
                                                isFinalCreated = 'Y'
                                          WHERE deliveryChallanDetailId =".$deliveryValue;
        $updateDeliveryChallanDetailRes = mysql_query($updateDeliveryChallanDetail);
      }
    }
    foreach ($_POST['deliveryChallanMasterId'] as $key => $value)
    {
      $updateDCDetail  = "UPDATE deliverychallandetail
                             SET masterDeliveryId = 'PM'
                           WHERE deliveryChallanMasterId = ".$value."
                             AND masterDeliveryId IS NULL";
      $updateDCDetailRes = mysql_query($updateDCDetail);
    }
//    if(!$updateDeliveryChallanDetailRes || !$updateDCDetailRes)
//      die("Insert Query Not Inserted : ".mysql_error());
//    else
//      header("Location:masterDeliveryList.php?partyId=".$_POST['partyId']);
//      exit();
  }
  // here Update Query For deliverychallandetail,deliverychallanmaster Table:End//
    
  ///// Select Party : Start
  $partyId       = array();
  $companyName   = array();
  
  $selectParty = "SELECT party.partyId AS pid,party.companyName
                    FROM party
                   WHERE party.partyId IN (SELECT partyId FROM deliverychallanmaster)
                   ORDER BY companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  { 

    $partyId[$b]     = $partyRow['pid']."P";
    $companyName[$b] = $partyRow['companyName'];
    $b++;
  }
  ///// Select Party : End
  include("./bottom.php");
  $smarty->assign("partyId",$partyId);
  $smarty->assign("msg",$msg);
  $smarty->assign("companyName",$companyName);
  $smarty->display("masterDelivery.tpl");
}
?>